package education.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import education.dao.MajorsInfoDao;
import education.model.Course;
import education.model.MajorsInfo;

@Component("majorsInfoDao")
public class MajorsInfoDaoImpl implements MajorsInfoDao {
	private HibernateTemplate hibernateTemplate;

	public boolean delete(int id) {
		Session session = hibernateTemplate.getSessionFactory()
				.openSession();
		Query query = session
				.createQuery("delete MajorsInfo majorsInfo where majorsInfo.id=?");
		query.setInteger(0, id);				
		int re = query.executeUpdate();
		session.close();
		if (re != 0) {
			return true;
		}
		System.out.println("MajorsInfoDaoImpl");
		return false;
	}

	public void delete(MajorsInfo majorsInfo) {
		hibernateTemplate.delete(majorsInfo);
	}

	public int getCount() {
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session.createQuery("select count(*) from MajorsInfo");
		String r = query.list().get(0).toString();
		int count = Integer.parseInt(r);
		session.close();
		return count;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public MajorsInfo loadById(int id) {
		Session session = hibernateTemplate.getSessionFactory()
				.getCurrentSession();

		return (MajorsInfo) session.get(MajorsInfo.class, id);
	}

	public List<MajorsInfo> paging(int pageSize, int pageCount, int rowCount) {
		List<MajorsInfo> list;
		pageSize = (pageSize - 1) * rowCount;
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session.createQuery("from MajorsInfo").setFirstResult(
				pageSize).setMaxResults(rowCount);
		list = query.list();
		session.close();
		return list;
	}

	public void save(MajorsInfo majorsInfo) {
		hibernateTemplate.save(majorsInfo);
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}


	public boolean update(MajorsInfo majorsInfo) {
		hibernateTemplate.update(majorsInfo);
		return true;
	}

	@SuppressWarnings("unchecked")
	public List<MajorsInfo> getCourseList() {
		return hibernateTemplate.find("from MajorsInfo");
	}

}
